package com.example.apple.ddhr_new.activity.widget;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.utils.StringUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Dialog {

    public static void alert(final Context context, String title, String msg, OnDialogOkClickListener onDialogCancelClickListener, OnDialogOkClickListener onDialogOkClickListener)
    {
        View view = View.inflate(context, R.layout.dialog_double_button, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setView(view);
        final AlertDialog dialog = builder.create();
        dialog.show();

        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_msg = view.findViewById(R.id.tv_msg);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);


        TextView tv_ok = view.findViewById(R.id.tv_ok);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        WindowManager m = ((Activity) context).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        if(StringUtils.isEmpty(title)){
            tv_title.setVisibility(View.GONE);

        }
        if(StringUtils.isEmpty(msg)){
            tv_msg.setVisibility(View.GONE);

        }
        layoutParams.width = (int) (d.getWidth() * 0.8);
//        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setBackgroundDrawableResource(R.drawable.shape_dialog_bg);
        window.setAttributes(layoutParams);
        window.setDimAmount(0.0f);
        //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏
        dialog.setCanceledOnTouchOutside(true);
        //设置按钮是否可以按返回键取消,false则不可以取消
        dialog.setCancelable(false);

        tv_title.setText(title);
        tv_msg.setText(msg);


        tv_title.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);

        tv_cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != dialog && dialog.isShowing() && !((Activity) context).isFinishing())
                {


                    if (null != dialog && dialog.isShowing() && !((Activity) context).isFinishing())
                    {
                        dialog.dismiss();
                    }

                    if (null != onDialogCancelClickListener)
                    {
                        onDialogCancelClickListener.onClick(null);
                    }
                }
            }
        });

        tv_ok.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != dialog && dialog.isShowing() && !((Activity) context).isFinishing())
                {
                    dialog.dismiss();
                }

                if (null != onDialogOkClickListener)
                {
                    onDialogOkClickListener.onClick(null);
                }
            }
        });

    }
    public static void alert(final Context context, String title, String msg, OnDialogOkClickListener onDialogOkClickListener)
    {
        View view = View.inflate(context, R.layout.dialog_single_button, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setView(view);
        final AlertDialog dialog = builder.create();
        dialog.show();

        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_msg = view.findViewById(R.id.tv_msg);



        TextView tv_ok = view.findViewById(R.id.tv_ok);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        WindowManager m = ((Activity) context).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        if(StringUtils.isEmpty(title)){
            tv_title.setVisibility(View.GONE);

        }
        if(StringUtils.isEmpty(msg)){
            tv_msg.setVisibility(View.GONE);

        }
        layoutParams.width = (int) (d.getWidth() * 0.8);
//        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setBackgroundDrawableResource(R.drawable.shape_dialog_bg);
        window.setAttributes(layoutParams);
        window.setDimAmount(0.0f);
        //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏
        dialog.setCanceledOnTouchOutside(true);
        //设置按钮是否可以按返回键取消,false则不可以取消
        dialog.setCancelable(false);

        tv_title.setText(title);
        tv_msg.setText(msg);


        tv_title.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);



        tv_ok.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != dialog && dialog.isShowing() && !((Activity) context).isFinishing())
                {
                    dialog.dismiss();
                }

                if (null != onDialogOkClickListener)
                {
                    onDialogOkClickListener.onClick(null);
                }
            }
        });

    }
    public interface OnDialogOkClickListener<T>
    {
        void onClick(T t);
    }


    public static OptionsPickerView popWheel(Context context,OnOptionsSelectListener listener){
        OptionsPickerView option=  new OptionsPickerBuilder(context, listener).setSubmitText(context.getString(R.string.sure))//确定按钮文字
                .setCancelText(context.getString(R.string.cacel))//取消按钮文字
                .setTitleText("")//标题
                .setSubCalSize(18)//确定和取消文字大小
                .setTitleColor(context.getResources().getColor(R.color.color_cell_title))//标题文字颜色
                .setSubmitColor(context.getResources().getColor(R.color.color_cell_title))//确定按钮文字颜色
                .setCancelColor(context.getResources().getColor(R.color.color_cell_title))//取消按钮文字颜色
                .setTitleBgColor(context.getResources().getColor(R.color.color_btn_disable))//标题背景颜色 Night mode

                .setBgColor( context.getResources().getColor( R.color.white))//滚轮背景颜色 Night mode
                .setContentTextSize(16)//滚轮文字大小
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setOutSideCancelable(false)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier(2.2f)
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .build();

        return  option;
    }
    public static TimePickerView popTime(Context context, Calendar min,Calendar max ,OnTimeSelectListener listener){
        TimePickerView builder =  new TimePickerBuilder(context, listener).setCancelText(context.getString(R.string.cacel))//取消按钮文字
                .setSubmitText(context.getString(R.string.sure))//确认按钮文字
                .setContentTextSize(16)//滚轮文字大小
                .setTitleSize(18)//标题文字大小
                .setTitleText(context.getString(R.string.xuanze_shijian))//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTitleColor(context.getResources().getColor(R.color.color_cell_title))//标题文字颜色
                .setSubmitColor(context.getResources().getColor(R.color.color_cell_title))//确定按钮文字颜色
                .setCancelColor(context.getResources().getColor(R.color.color_cell_title))//取消按钮文字颜色
                .setTitleBgColor(context.getResources().getColor(R.color.color_btn_disable))//标题背景颜色 Night mode
                .setBgColor(context.getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                .setRangDate(min,max)
                .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                .setType(new boolean[]{true,true,true,false,false,false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier(2.2f)
                .build();
        return builder;
    }
    public static TimePickerView popMinuteTime(Context context,Calendar min,Calendar max, OnTimeSelectListener listener){
        TimePickerView builder =  new TimePickerBuilder(context, listener).setCancelText(context.getString(R.string.cacel))//取消按钮文字
                .setSubmitText(context.getString(R.string.sure))//确认按钮文字
                .setContentTextSize(16)//滚轮文字大小
                .setTitleSize(18)//标题文字大小
                .setTitleText(context.getString(R.string.xuanze_shijian))//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTitleColor(context.getResources().getColor(R.color.color_cell_title))//标题文字颜色
                .setSubmitColor(context.getResources().getColor(R.color.color_cell_title))//确定按钮文字颜色
                .setCancelColor(context.getResources().getColor(R.color.color_cell_title))//取消按钮文字颜色
                .setTitleBgColor(context.getResources().getColor(R.color.color_btn_disable))//标题背景颜色 Night mode
                .setBgColor(context.getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                .setRangDate(min,max)
                .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                .setType(new boolean[]{false,false,false,true,true,false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier(2.2f)
                .build();
        return builder;
    }


}

